﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using BPM.Controllers;
using System.Web.Mvc;
using BPM.Common;
using Moq;
using BPM.Entity;
using BPM.Service;

namespace WebTest.Controllers
{
	[TestClass]
	public class CategoryTest : ControllerTestBase
	{
		[ClassInitialize()]
		public static void ClassInitialize(TestContext testContext)
		{
			InitializeApplication();
		}

		[TestInitialize()]
		public override void TestInitialize()
		{
			LogonAsAdmin();

			this._controller = CreateController<CategoryController>();
		}

		[TestCleanup()]
		public override void TestCleanup()
		{
			Logout();
		}

		private CategoryController _controller;

		[TestMethod]
		public void CategoryTest_AddCate()
		{
			//add cate
			var r = this._controller.AddCate("test");
			AssertIsTrue(r);
			//no id returns, so unit test can't delete the previous entity...

		}

		[TestMethod]
		public void CategoryTest_AddSubCate_Edit_Delete()
		{
			//add sub cate
			var svc = new MaterialDefinitionCategoryService();
			MaterialDefinitionCategory entity = null;
			var res = svc.Create("testCate", out entity);
			Assert.IsTrue(res);
			var r = this._controller.AddSubCate(entity.Id, "testSubCate");
			AssertIsTrue(r);

			//Edit
			r = this._controller.EditCate(entity.Id, "testCate2");
			AssertIsTrue(r);
			svc = new MaterialDefinitionCategoryService();
			entity = svc.GetById(entity.Id);
			Assert.AreEqual(entity.Name, "testCate2");

			//Delete these two categories.
			svc = new MaterialDefinitionCategoryService();
			entity = svc.GetById(entity.Id);
			Assert.IsNotNull(entity.SubCategoryList);
			Assert.AreEqual(entity.SubCategoryList.Count, 1);
			r = this._controller.Delete(entity.SubCategoryList.First().Id);
			AssertIsTrue(r);
			r = this._controller.Delete(entity.Id);
			AssertIsTrue(r);
		}
	}
}
